﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c)
*/
#region Using

using System.Data;
using System.Data.Common;
using NetBay.Core.BusinessEntities.Workflow;
using Rio.Flow.Business.Repository.Translator;
using Rio.Flow.DataAccess;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using Rio.Doc.Business.Repository.Translator;
using Rio.Doc.Business.Repository;
using NetBay.Core.Extensions;
#endregion

namespace Rio.Flow.Business.Repository
{
    /// <summary>
    /// Message repository
    /// </summary>
    public class WorkflowInstanceRepository
    {

        #region Public functions

        /// <summary>
        /// Add an instance
        /// </summary>
        /// <param name="wkfDefinition">The WKF definition.</param>
        /// <param name="folderId">Folder identifier</param>
        /// <param name="userId">User identifier</param>
        /// <param name="state">State</param>
        /// <param name="startingCondition">The starting condition.</param>
        /// <param name="docId">RIO Folder identifier</param>
        /// <param name="ChronoId">The chrono id.</param>
        /// <param name="parentId">Parent instance identifier</param>
        /// <param name="receiversGroupIds">The receivers group ids.</param>
        /// <returns></returns>
        public static long AddWorkflowInstance(WorkflowDefinition wkfDefinition, long folderId, long userId, WorkflowState state, long startingCondition, List<long> docId, long? ChronoId = 0, long? parentId = null, List<long> receiversGroupIds = null)
        {
            return DA_WorkflowInstance.AddWorkflowInstance(wkfDefinition, folderId, userId, state, startingCondition, docId, ChronoId, parentId, receiversGroupIds);
        }

        /// <summary>
        /// Adds the and get workflow instance.
        /// </summary>
        /// <param name="wkfDefinition">The WKF definition.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="state">The state.</param>
        /// <param name="startingCondition">The starting condition.</param>
        /// <param name="docId">The doc id.</param>
        /// <param name="ChronoId">The chrono id.</param>
        /// <param name="parentId">The parent id.</param>
        /// <returns></returns>
        public static WorkflowInstance AddAndGetWorkflowInstance(WorkflowDefinition wkfDefinition, long folderId, long userId, WorkflowState state, long startingCondition, List<long> docId, long? ChronoId = 0, long? parentId = null)
        {
            long wkfInstanceId = AddWorkflowInstance(wkfDefinition, folderId, userId, state, startingCondition, docId, ChronoId, parentId);
            if (wkfInstanceId > 0)
            {
                return GetWorkflowInstance(wkfInstanceId, userId);
            }
            return null;
        }

        /// <summary>
        /// Adds the and get workflow instance with receivers.
        /// </summary>
        /// <param name="wkfDefinition">The WKF definition.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="state">The state.</param>
        /// <param name="startingCondition">The starting condition.</param>
        /// <param name="docId">The doc id.</param>
        /// <param name="ChronoId">The chrono id.</param>
        /// <param name="parentId">The parent id.</param>
        /// <param name="receiversGroupIds">The receivers group ids.</param>
        /// <returns></returns>
        public static WorkflowInstance AddAndGetWorkflowInstanceWithReceivers(WorkflowDefinition wkfDefinition, long folderId, long userId, WorkflowState state, long startingCondition, List<long> docId, long? ChronoId = 0, long? parentId = null, List<long> receiversGroupIds = null)
        {
            long wkfInstanceId = AddWorkflowInstance(wkfDefinition, folderId, userId, state, startingCondition, docId, ChronoId, parentId, receiversGroupIds);
            if (wkfInstanceId > 0)
            {
                return GetWorkflowInstance(wkfInstanceId, userId);
            }
            return null;
        }

        /// <summary>
        /// Gets the workflow instances by definition id.
        /// </summary>
        /// <param name="wkfDefinitionId">The WKF definition id.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesByDefinitionId(long wkfDefinitionId)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesByDefinitionId(wkfDefinitionId));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
                inst.Definition = WorkflowDefinitionRepository.GetWorkflowDefinitionById(inst.DefinitionId);
            }

            return instances;
        }

        /// <summary>
        /// Gets the workflow instance by folder id.
        /// </summary>
        /// <param name="folderTypeId">The folder type id.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="isFolderTypeAdmin">The is folder type admin.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesByFolderId(long folderTypeId, long folderId, long userId, bool? isFolderTypeAdmin = false)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesByFolderId(folderTypeId, folderId, userId, isFolderTypeAdmin));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
            }
            return instances;
        }

        /// <summary>
        /// Gets the workflow instance.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="wkfInstanceIsRead">if set to <c>true</c> [WKF instance is read].</param>
        /// <param name="wkfUserInstanceIsRead">if set to <c>true</c> [WKF user instance is read].</param>
        /// <returns></returns>
        public static WorkflowInstance GetWorkflowInstance(long wkfInstanceId, long userId, bool wkfInstanceIsRead = false, bool wkfUserInstanceIsRead = false)
        {
            var instance = WorkflowInstanceTranslator.TranslateWorkflowInstance(DA_WorkflowInstance.GetWorkflowInstance(wkfInstanceId, userId, wkfInstanceIsRead, wkfUserInstanceIsRead));
            instance.WorkItems = GetDocumentState(instance);
            instance.Receivers = GetReceivers(instance.Identifier);
            instance.Definition = WorkflowDefinitionRepository.GetWorkflowDefinitionById(instance.DefinitionId);
            return instance;
        }

        /// <summary>
        /// Gets the workflow child instance.
        /// </summary>
        /// <param name="folderTypeId">The folder type id.</param>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetChildWorkflowInstance(long folderTypeId, long wkfInstanceId)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetChildWorkflowInstance(wkfInstanceId));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
                inst.Definition = WorkflowDefinitionRepository.GetWorkflowDefinitionById(inst.DefinitionId);
                inst.FolderIndex = IndexRepository.GetFolderIndexValues(folderTypeId, inst.FolderId);
            }
            return instances;
        }

        /// <summary>
        /// Gets the workflow parent instance.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static WorkflowInstance GetParentWorkflowInstance(long wkfInstanceId)
        {
            var instance = WorkflowInstanceTranslator.TranslateWorkflowInstance(DA_WorkflowInstance.GetParentWorkflowInstance(wkfInstanceId));
            instance.WorkItems = GetDocumentState(instance);
            instance.Receivers = GetReceivers(instance.Identifier);
            instance.Definition = WorkflowDefinitionRepository.GetWorkflowDefinitionById(instance.DefinitionId);
            return instance;
        }

        /// <summary>
        /// Gets the workflow instances by sender id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="folderTypeId">The folder type id.</param>
        /// <param name="completedInstance">The completed instance.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesBySenderId(long userId, long folderTypeId, bool completedInstance)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesBySenderId(userId, folderTypeId, completedInstance));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
                inst.FolderIndex = IndexRepository.GetFolderIndexValues(folderTypeId, inst.FolderId);
            }
            return instances;
        }

        /// <summary>
        /// Gets the workflow instances by receiver id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="folderTypeId">The folder type id.</param>
        /// <param name="completedInstance">The completed instance.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesByReceiverId(long userId, long folderTypeId, bool completedInstance)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesByReceiverId(userId, folderTypeId, completedInstance));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
                inst.FolderIndex = IndexRepository.GetFolderIndexValues(folderTypeId, inst.FolderId);
            }
            return instances;
        }

        /// <summary>
        /// Gets the instances by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesByUserId(long userId)
        {
            return WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesByUserId(userId));
        }

        /// <summary>
        /// Gets the workflow instances by FLD type id.
        /// </summary>
        /// <param name="folderTypeId">The folder type id.</param>
        /// <param name="completedInstance">The completed instance.</param>
        /// <returns></returns>
        public static List<WorkflowInstance> GetWorkflowInstancesByFldTypeId(long folderTypeId, bool completedInstance)
        {
            var instances = WorkflowInstanceTranslator.TranslateWorkflowInstances(DA_WorkflowInstance.GetWorkflowInstancesByFldTypeId(folderTypeId, completedInstance));
            foreach (WorkflowInstance inst in instances)
            {
                inst.Receivers = GetReceivers(inst.Identifier);
                inst.FolderIndex = IndexRepository.GetFolderIndexValues(folderTypeId, inst.FolderId);
            }
            return instances;
        }

        /// <summary>
        /// Returns the receivers for the instance id.
        /// </summary>
        /// <returns></returns>
        public static string GetReceivers(long wkfInstanceId)
        {
            return DA_WorkflowInstance.GetReceivers(wkfInstanceId);
        }

        /// <summary>
        /// Adds the receivers.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="receiversGroupIds">The receivers group ids.</param>
        /// <returns></returns>
        public static bool AddReceivers(long wkfInstanceId, List<long> receiversGroupIds)
        {
            return DA_WorkflowInstance.AddUserInstances(wkfInstanceId, receiversGroupIds);
        }

        /// <summary>
        /// Adds an instancelink.
        /// </summary>
        /// <param name="wkfInstanceIdParent">The WKF instance id parent.</param>
        /// <param name="wkfInstanceIdChild">The WKF instance id child.</param>
        /// <returns></returns>
        public static bool AddInstanceLink(long wkfInstanceIdParent, long wkfInstanceIdChild)
        {
            return DA_WorkflowInstance.AddInstanceLink(wkfInstanceIdParent, wkfInstanceIdChild);
        }

        /// <summary>
        /// Returns all documents.
        /// </summary>
        /// <returns></returns>
        public static List<WorkflowItem> GetDocumentState(WorkflowInstance wkfInstance)
        {
            List<WorkflowItem> documents = WorkflowInstanceTranslator.TranslateDocumentState(DA_WorkflowInstance.GetDocumentState(wkfInstance.Identifier));
            if (documents.Count == 0)
            {
                documents.Add(new WorkflowGenericItem
                                  {
                        Name = wkfInstance.StartingConditionLabel,
                    });
            }
            return documents;
        }

        /// <summary>
        /// Gets the document state by user instance.
        /// </summary>
        /// <param name="userInstance">The user instance.</param>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static Dictionary<long, long> GetDocumentStateByUserInstance(long userInstance, long wkfInstanceId, long rguId)
        {
            Dictionary<long, long> result = new Dictionary<long, long>();

            IDataReader DbDataReader  = DA_WorkflowInstance.GetDocumentStateByUserInstance(userInstance, wkfInstanceId, rguId);
            using (DbDataReader)
            {
                // Read all rows
                while (DbDataReader.Read())
                {
                    result.Add(DbDataReader.GetLongValue("rd_no"), DbDataReader.GetLongValue("t_ref_wkf_state_id"));
                }
            }

            return result;
        }

        /// <summary>
        /// Gets the user instances.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static List<WorkflowInstanceUser> GetUserInstances(long wkfInstanceId)
        {
            return WorkflowInstanceTranslator.TranslateUserInstances(DA_WorkflowInstance.GetUserInstances(wkfInstanceId));
        }

        /// <summary>
        /// Update state of a document.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="wkfDocStateId">The WKF doc state id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="actionOrConditionId">The action or condition id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public static bool UpdateDocumentStateById(long wkfInstanceId, long wkfDocStateId, WorkflowState wkfState, long? rguId, long? userId, long actionOrConditionId, string comment = "")
        {
            return DA_WorkflowInstance.UpdateDocumentStateById(wkfInstanceId, wkfDocStateId, wkfState, rguId, userId, actionOrConditionId, comment);
        }

        /// <summary>
        /// Updates the document state by inst.,
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="actionOrConditionId">The action or condition id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public static bool UpdateDocumentStateByInst(long wkfInstanceId, WorkflowState wkfState, long? rguId, long? userId,long actionOrConditionId, string comment = "")
        {
            return DA_WorkflowInstance.UpdateDocumentStateByInstanceId(wkfInstanceId, wkfState, rguId, userId, actionOrConditionId, comment);
        }

        /// <summary>
        /// Update state of an instance.
        /// </summary>
        /// <param name="wkfDefinitionId">The WKF definition id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="isEnded">if set to <c>true</c> [is ended].</param>
        /// <returns></returns>
        public static bool UpdateInstance(long wkfDefinitionId, WorkflowState wkfState, long? rguId, long? userId, bool isEnded = false)
        {
            return DA_WorkflowInstance.UpdateInstance(wkfDefinitionId, wkfState, userId, rguId, isEnded);
        }

        /// <summary>
        /// Deletes the instance.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static bool DeleteInstance(long wkfInstanceId)
        {
            return DA_WorkflowInstance.DeleteInstance(wkfInstanceId);
        }

        /// <summary>
        /// Deletes the user instance.
        /// </summary>
        /// <param name="wkfUserInstanceId">The WKF user instance id.</param>
        /// <returns></returns>
        public static bool DeleteUserInstance(long wkfUserInstanceId)
        {
            return DA_WorkflowInstance.DeleteUserInstance(wkfUserInstanceId);
        }

        /// <summary>
        /// Updates the receivers action.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rguid">The rguid.</param>
        /// <param name="actionId">The action id.</param>
        /// <returns></returns>
        public static bool UpdateReceiversAction(long wkfInstanceId, long rguid, long actionId)
        {
            return DA_WorkflowInstance.UpdateReceiversAction(wkfInstanceId, rguid, actionId);
        }

        /// <summary>
        /// Updates the receivers to ended action.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rguid">The rguid.</param>
        /// <param name="isEnded">The is ended.</param>
        /// <returns></returns>
        public static bool UpdateReceiversToEnded(long wkfInstanceId, long rguid, bool isEnded)
        {
            return DA_WorkflowInstance.UpdateReceiversToEnded(wkfInstanceId, rguid, isEnded);
        }

        /// <summary>
        /// Updates the instance to readed.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static bool UpdateInstanceToReaded(long wkfInstanceId)
        {
            return DA_WorkflowInstance.UpdateInstanceToReaded(wkfInstanceId);
        }

        /// <summary>
        /// Updates the user instance to readed.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="UserId">The user id.</param>
        /// <returns></returns>
        public static bool UpdateUserInstanceToReaded(long wkfInstanceId, long UserId)
        {
            return DA_WorkflowInstance.UpdateUserInstanceToReaded(wkfInstanceId, UserId);
        }

        /// <summary>
        /// Gets the workflow instances count by sender id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="completedInstance">if set to <c>true</c> [completed instance].</param>
        /// <returns></returns>
        public static List<WkfTuple> GetInstancesCountBySenderId(long userId, bool completedInstance)
        {
            return WorkflowInstanceTranslator.TranslateTuple(DA_WorkflowInstance.GetWorkflowInstancesCountBySenderId(userId, completedInstance));
        }

        /// <summary>
        /// Gets the instances count by receiver id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="completedInstance">The completed instance.</param>
        /// <returns></returns>
        public static List<WkfTuple> GetInstancesCountByReceiverId(long userId, bool completedInstance)
        {
            return WorkflowInstanceTranslator.TranslateTuple(DA_WorkflowInstance.GetWorkflowInstancesCountByReceiverId(userId, completedInstance));
        }

        /// <summary>
        /// Gets the workflow instance not readed.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static long GetWorkflowInstanceNotRead(long userId)
        {
            return DA_WorkflowInstance.GetWorkflowInstanceNotRead(userId);
        }

        /// <summary>
        /// Adds the state of the document.
        /// </summary>
        /// <param name="DocumentId">The document id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static bool AddDocumentState(long DocumentId, WorkflowState wkfState, long wkfInstanceId)
        {
            return DA_WorkflowInstance.AddDocumentState(DocumentId, wkfState, wkfInstanceId);
        }

        /// <summary>
        /// Gets the historical.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <returns></returns>
        public static List<WorkflowHisto> GetHistorical(long wkfInstanceId)
        {
            return WorkflowInstanceTranslator.TranslateHisto(DA_WorkflowInstance.GetHisto(wkfInstanceId));
        }

        /// <summary>
        /// Gets the histo by doc id.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rdNo">The rd no.</param>
        /// <returns></returns>
        public static List<WorkflowHisto> GetHistoByDocId(long wkfInstanceId, long rdNo)
        {
            return WorkflowInstanceTranslator.TranslateHisto(DA_WorkflowInstance.GetHistoByDocId(wkfInstanceId, rdNo));
        }

        /// <summary>
        /// Gets the last receivers action.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="rdNo">The rd no.</param>
        /// <returns></returns>
        public static long GetLastReceiversAction(long wkfInstanceId, long rguId, long? rdNo)
        {
            return DA_WorkflowInstance.GetLastReceiversAction(wkfInstanceId, rguId, rdNo);
        }

        /// <summary>
        /// Updates the instance to ended.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="isEnded">The is ended.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static bool UpdateInstanceToEnded(long wkfInstanceId, bool isEnded, long? rguId, long? userId)
        {
            return DA_WorkflowInstance.UpdateInstanceToEnded(wkfInstanceId, isEnded, rguId, userId);
        }

        /// <summary>
        /// Restarts the workflow instance.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="newReceivers">The new receivers.</param>
        /// <returns></returns>
        public static bool RestartWorkflowInstance(long wkfInstanceId, string newReceivers)
        {
            return DA_WorkflowInstance.RestartWorkflowInstance(wkfInstanceId, newReceivers);
        }

        /// <summary>
        /// Gets the workflow document by folder.
        /// </summary>
        /// <param name="folderTypeID">The folder type ID.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <returns></returns>
        public static List<Document> GetWorkflowDocumentsByInstance(long folderTypeID, long folderId, long rguId, long userId, long wkfInstanceId, WorkflowState wkfState)
        {
            // Finaly traslate results into a list of documents
            return DocumentTranslator.TranslateDocuments(DA_WorkflowInstance.GetWorkflowDocumentsByInstance(folderTypeID, folderId, rguId, userId, wkfInstanceId, wkfState));
        }

        /// <summary>
        /// Gets the workflow document by folder.
        /// </summary>
        /// <param name="folderTypeID">The folder type ID.</param>
        /// <param name="folderId">The folder id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="wkfUserInstanceId">The WKF user instance id.</param>
        /// <param name="wkfState">State of the WKF.</param>
        /// <returns></returns>
        public static List<Document> GetWorkflowDocumentsByUser(long folderTypeID, long folderId, long rguId, long userId, long wkfUserInstanceId, WorkflowState wkfState)
        {
            // Finaly traslate results into a list of documents
            return DocumentTranslator.TranslateDocuments(DA_WorkflowInstance.GetWorkflowDocumentsByUser(folderTypeID, folderId, rguId, userId, wkfUserInstanceId, wkfState));
        }

        /// <summary>
        /// Adds the historical.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="state">The state.</param>
        /// <param name="actionIdentifier">The action identifier.</param>
        /// <param name="documentIdentifier">The document identifier.</param>
        /// <returns></returns>
        public static bool AddHistorical(long wkfInstanceId, long? rguId, long userId, WorkflowState state, long? actionIdentifier, long? documentIdentifier)
        {
            return DA_WorkflowInstance.AddHistorical(wkfInstanceId, rguId, userId, state, actionIdentifier, documentIdentifier);
        }

        /// <summary>
        /// Adds the historical for all documents.
        /// </summary>
        /// <param name="wkfInstanceId">The WKF instance id.</param>
        /// <param name="rguId">The rgu id.</param>
        /// <param name="userId">The user id.</param>
        /// <param name="state">The state.</param>
        /// <param name="actionIdentifier">The action identifier.</param>
        /// <returns></returns>
        public static bool AddHistoricalForAllDocuments(long wkfInstanceId, long? rguId, long userId,  WorkflowState state, long? actionIdentifier)
        {
            return DA_WorkflowInstance.AddHistoricalForAllDocuments(wkfInstanceId, rguId, userId, state, actionIdentifier);    
        }

        /// <summary>
        /// Gets the document instances list.
        /// </summary>
        /// <param name="documentIdentifier">The document identifier.</param>
        /// <param name="deletedInstances">if set to <c>true</c> [deleted instances].</param>
        /// <returns></returns>
        public static List<long> GetDocumentInstancesList(long documentIdentifier, bool deletedInstances)
        {
            return DA_WorkflowInstance.GetDocumentInstancesList(documentIdentifier, deletedInstances);
        }

        /// <summary>
        /// Documents the has WKF instances started.
        /// </summary>
        /// <param name="documentIdentifier">The document identifier.</param>
        /// <returns></returns>
        public static bool DocumentHasWkfInstancesStarted(long documentIdentifier)
        {
            List<long> result = DA_WorkflowInstance.GetDocumentInstancesList(documentIdentifier, false);
            if (result.Count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Updates the state of the user instance doc.
        /// </summary>
        /// <param name="wkfUserInstanceId">The WKF user instance id.</param>
        /// <param name="docStateId">The doc state id.</param>
        /// <param name="newState">The new state.</param>
        /// <returns></returns>
        public static bool UpdateUserInstanceDocState(long wkfUserInstanceId, long? docStateId, WorkflowState newState)
        {
            return DA_WorkflowInstance.UpdateUserInstanceDocState(wkfUserInstanceId, docStateId, newState);
        }

        /// <summary>
        /// Gets the workflow instance by documents id.
        /// </summary>
        /// <param name="documentIds">The document ids.</param>
        /// <returns></returns>
        public static List<long> GetDocumentsIdsInWorkfow(List<long> documentIds)
        {
            List<long> result = new List<long>();
            foreach (long documentId in documentIds)
            {
                var instanceId = GetUserInstanceDocStateByDocumentsId(documentId);
                if(instanceId.Count > 0)
                    result.Add(documentId);
            }
            return result;
        }

        /// <summary>
        /// Gets the workflow instance by documents id.
        /// </summary>
        /// <param name="documentId">The document id.</param>
        /// <returns></returns>
        public static List<long> GetUserInstanceDocStateByDocumentsId(long documentId)
        {
            return DA_WorkflowInstance.GetUserInstanceDocStateByDocumentsId(documentId);
        }

        #endregion

        #region Private functions
        #endregion

    }
}